/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.checker;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.jdt.core.dom.InfixExpression.Operator;

import com.googlecode.syper.javasyp.formulae.Field;
import com.googlecode.syper.javasyp.formulae.Formula;
import com.googlecode.syper.javasyp.formulae.FormulaVisitor;
import com.googlecode.syper.javasyp.formulae.Fun;
import com.googlecode.syper.javasyp.formulae.IfThenElse;
import com.googlecode.syper.javasyp.formulae.Infix;
import com.googlecode.syper.javasyp.formulae.Literal;
import com.googlecode.syper.javasyp.formulae.Local;
import com.googlecode.syper.javasyp.formulae.Pre;
import com.googlecode.syper.javasyp.formulae.Prefix;
import com.googlecode.syper.javasyp.formulae.Quantifier;
import com.googlecode.syper.javasyp.formulae.Select;
import com.googlecode.syper.javasyp.formulae.Store;
import com.googlecode.syper.javasyp.prover.Prover;
import com.googlecode.syper.javasyp.prover.ProverResult;

/**
 * @author kevin
 * @since Sep 23, 2010
 */
public class FailingFinder extends FormulaVisitor<Boolean> {

	private final Prover prover;
	private final Collection<? extends Formula> knowledge;
	private final Set<Formula> result;

	public static Set<Formula> findFailing(Prover p,
			Collection<? extends Formula> knowledge,
			Formula assertion) {
		FailingFinder f = new FailingFinder(p, knowledge);
		assertion.dispatch(f);
		return f.result;
	}

	private FailingFinder(Prover prover, Collection<? extends Formula> knowledge) {
		this.prover = prover;
		this.knowledge = knowledge;
		this.result = new LinkedHashSet<Formula>();
	}

	private Boolean check(Formula f) {
		try {
			boolean r = prover.check(knowledge, f).getStatus() == ProverResult.Status.UNSAT;
			if (!r)
				result.add(f);
			return r;
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			return false;
		}
	}

	@Override
	public Boolean visit(Local f) {
		return check(f);
	}

	@Override
	public Boolean visit(Field f) {
		return check(f);
	}

	@Override
	public Boolean visit(Infix f) {
		if (Operator.AND.equals(f.getOp())) {
			Boolean left = f.getLhs().dispatch(this);
			Boolean right = f.getRhs().dispatch(this);
			return left & right;
		} else if (Operator.CONDITIONAL_OR.equals(f.getOp())) {
			Boolean left = f.getLhs().dispatch(this);
			if (left)
				return true;
			// TODO assume lhs false
			return f.getRhs().dispatch(this);
		} else if (Operator.CONDITIONAL_AND.equals(f.getOp())) {
			Boolean left = f.getLhs().dispatch(this);
			if (!left)
				return false;
			// TODO assume lhs true
			return f.getRhs().dispatch(this);
		} else {
			return check(f);
		}
	}

	@Override
	public Boolean visit(Literal f) {
		return check(f);
	}

	@Override
	public Boolean visit(Prefix f) {
		// TODO descend
		return check(f);
	}

	@Override
	public Boolean visit(Select f) {
		return check(f);
	}

	@Override
	public Boolean visit(Store f) {
		return check(f);
	}

	@Override
	public Boolean visit(Pre f) {
		return check(f);
	}

	@Override
	public Boolean visit(Quantifier f) {
		return check(f);
	}

	@Override
	public Boolean visit(Fun f) {
		return check(f);
	}

	@Override
	public Boolean visit(IfThenElse f) {
		// TODO descend
		return check(f);
	}
}
